#include "StdAfx.h"
#include "xtCapMan.h"
#include "xhHubCap.h"
#include "xtCalcAH.h"

#include "DDefaultLineCap.h"
#define TestCapture "Test-Cap"

xtCapMan::xtCapMan(void)
{
	m_hPlugDll     = NULL;
	m_pLineCapPlug = NULL;

	m_bLoadCap  = FALSE;
	m_nDevCount = 0;
	memset(&m_xUseDevDesc,0,sizeof(m_xUseDevDesc));

	char szModulePath[MAX_PATH] = "";
	GetModuleFileName(NULL,szModulePath,MAX_PATH);
	char* sTemp = strrchr(szModulePath,'\\'); 
	sTemp++;
	sTemp[0] = '\0'; 
	sprintf(m_szPlugDir,"%sPlugCap\\",szModulePath); 
}

xtCapMan::~xtCapMan(void)
{
	DD_AUTO_LOCK xLock(&m_CapSect);

	MySafeDelete( m_pLineCapPlug );
	MySafeFreeLib( m_hPlugDll );
}

int xtCapMan::LoadCaptureType()
{
	char szPlugPath[MAX_PATH] = "";
	char szPlugFile[MAX_PATH] = "";
	char szCurDir[MAX_PATH]   = "";

	::GetCurrentDirectory( MAX_PATH,szCurDir );
	::SetCurrentDirectory( m_szPlugDir );
	
	sprintf(szPlugPath,"%s\\*.cap",m_szPlugDir);


	m_nDevCount = 0;
	memset(&m_xDevDescs,0,sizeof(m_xDevDescs));
	int nDevCount = 0;

	HINSTANCE    hPlugDll = NULL;
	LineCapPlug *pPlug    = NULL;

	WIN32_FIND_DATA   filedata; 
	HANDLE            filehandle; 

	filehandle = FindFirstFile( szPlugPath,&filedata ); 

	if(filehandle!=INVALID_HANDLE_VALUE) 
	{ 
		do 
		{ 
			if( filedata.cFileName ) 
			{ 
				sprintf(szPlugFile,"%s\\%s",m_szPlugDir,filedata.cFileName);

				if( strcmp( m_xUseDevDesc.cDevDllName,szPlugFile ) != 0 )
				{
					MySafeDelete( pPlug );
					MySafeFreeLib( hPlugDll );
					{
						hPlugDll = LoadLibrary( szPlugFile );
						if(NULL != hPlugDll)
						{
							LPOPCREATEPLUGMAN CreatePlugMan = (LPOPCREATEPLUGMAN)GetProcAddress(hPlugDll, "CreatePlugMan");
							if( NULL != CreatePlugMan )
							{
								if( DD_ERR_SUCCESS == CreatePlugMan( &pPlug ) )
								{
									if( NULL != pPlug )
									{
										nDevCount = m_nDevCount;
										pPlug->GetPlugDescription( m_xDevDescs,nDevCount );

										for(int i=m_nDevCount;i<nDevCount;i++)
										{
											sprintf( m_xDevDescs[i].cDevDllName,szPlugFile);
											m_nDevCount++;
										}		
									}
								}
							}
						}
					}
					MySafeDelete( pPlug );
					MySafeFreeLib( hPlugDll );
				}
				else
				{
					if( NULL != m_pLineCapPlug )
					{
						nDevCount = m_nDevCount;
						m_pLineCapPlug->GetPlugDescription( m_xDevDescs,nDevCount );

						for(int i=m_nDevCount;i<nDevCount;i++)
						{
							sprintf( m_xDevDescs[i].cDevDllName,szPlugFile);
							m_nDevCount++;
						}	
					}
				}

			} 

		}while(FindNextFile(filehandle,&filedata)); 

		FindClose(filehandle); 
	} 

	::SetCurrentDirectory( szCurDir );

	if( m_nDevCount > 0 )
	{
		return 0;
	}

	sprintf( m_xDevDescs[0].cDeviceName,TestCapture );
	m_xDevDescs[0].ucNet = 0;

	m_nDevCount = 1;

	return -1;
}

int xtCapMan::SearchCapture()
{
	return 0;
}

int xtCapMan::GetCaptureList(zkCapDevDesc **ppDescs,int &nCount)
{	
	if( m_nDevCount <= 0 )
	{
		LoadCaptureType();
	}
	*ppDescs = m_xDevDescs;
	nCount   = m_nDevCount;

	return 0;
}


#define DKQ_R 1348

void xtCapMan::SetTestCapturePartAH()
{
	zkHubImgInfo xHub;
	xHub.n0 = 0;
	xHub.n1 = 1348;
	xHub.n2 = 1748;
	
	( (xhHubCap*)this )->SetPartInfo( &xHub );
}


int xtCapMan::SelectCapture( zkCapDevDesc *pDesc )
{
	DD_AUTO_LOCK xLock(&m_CapSect);

	if( NULL == pDesc )
	{
		return DD_ERR_FAILURE;
	}

	int nRet = -1;

	if( memcmp( &m_xUseDevDesc,pDesc,sizeof(zkCapDevDesc) )== 0 )
	{
		return DD_ERR_SUCCESS;
	}
	else if( strcmp(pDesc->cDeviceName,TestCapture) == 0 )
	{
		MySafeDelete( m_pLineCapPlug );
		MySafeFreeLib( m_hPlugDll );

		memset(&m_xUseDevDesc,0,sizeof(m_xUseDevDesc));

		m_pLineCapPlug = new DDefaultLineCap;

		if( DD_ERR_SUCCESS == InitPlug() )
		{
			memcpy( &m_xUseDevDesc,pDesc,sizeof(zkCapDevDesc) );

			SetTestCapturePartAH();

			return DD_ERR_SUCCESS;
		}
		else
		{
			MySafeDelete(m_pLineCapPlug);
			return DD_ERR_FAILURE;
		}
	}

	char szCurDir[MAX_PATH] = "";
	::GetCurrentDirectory( MAX_PATH,szCurDir );
	::SetCurrentDirectory( m_szPlugDir );


	if( DD_ERR_SUCCESS == LoadPlug( pDesc->cDevDllName ) )
	{
		nRet = m_pLineCapPlug->SelectCap( pDesc );
		if( DD_ERR_SUCCESS == nRet )
		{
			nRet = InitPlug();
			if( DD_ERR_SUCCESS == nRet )
			{
				memcpy( &m_xUseDevDesc,pDesc,sizeof(zkCapDevDesc) );
			}
		}
	}

	::SetCurrentDirectory( szCurDir );

	if( DD_ERR_SUCCESS != nRet )
	{
		MySafeDelete( m_pLineCapPlug );
		MySafeFreeLib( m_hPlugDll );

		memset(&m_xUseDevDesc,0,sizeof(m_xUseDevDesc));
	}

	return nRet;
}



int xtCapMan::LoadPlug( char *pFileName )
{
	int nRet = -1;

	MySafeDelete( m_pLineCapPlug );
	MySafeFreeLib( m_hPlugDll );

	memset(&m_xUseDevDesc,0,sizeof(m_xUseDevDesc));

	do 
	{
#ifdef _DKQ_TEST
		m_xLogInfo.WriteLog(__FILE__,__LINE__,"LoadLibrary pFileName:%s",pFileName);
#endif

		m_hPlugDll = LoadLibrary( pFileName );
		if(NULL == m_hPlugDll)
		{
#ifdef _DKQ_TEST
			m_xLogInfo.WriteLog(__FILE__,__LINE__,"ErrorCode:%d",GetLastError());
#endif
		}

#ifdef _DKQ_TEST
		m_xLogInfo.WriteLog(__FILE__,__LINE__,"GetProcAddress pFileName:%s",pFileName);
#endif


		LPOPCREATEPLUGMAN CreatePlugMan = (LPOPCREATEPLUGMAN)GetProcAddress(m_hPlugDll, "CreatePlugMan");	
		if( NULL == CreatePlugMan )
		{
			break;
		}

#ifdef _DKQ_TEST
		m_xLogInfo.WriteLog(__FILE__,__LINE__,"CreatePlugMan pFileName:%s",pFileName);
#endif

		if( DD_ERR_SUCCESS != CreatePlugMan( &m_pLineCapPlug ) )
		{
			MySafeFreeLib( m_hPlugDll );

			break;
		}

#ifdef _DKQ_TEST
		m_xLogInfo.WriteLog(__FILE__,__LINE__,"LoadPlug Success");
#endif

		return 0;

	}while( FALSE );

	MySafeDelete( m_pLineCapPlug );
	MySafeFreeLib( m_hPlugDll );

	return nRet;
}


int xtCapMan::InitPlug()
{
	if( NULL == m_pLineCapPlug )
	{
		return DD_ERR_NODEV;
	}

	return ( (xhHubCap*)this )->InitializePlug();
}